/**
 * 并行加载多个JS文件（public目录下）
 * @param {string[]} jsPaths - JS路径数组（绝对路径，如 ['/js/a.js', '/js/b.js']）
 * @returns {Promise} - 所有JS加载完成的Promise
 */
export const loadScripts = (jsPaths) => {
  const paths = Array.isArray(jsPaths) ? jsPaths : [jsPaths]
  const loadPromises = []

  paths.forEach((path) => {
    // 检查是否已加载（防重复）
    if (document.querySelector(`script[src="${path}"]`)) {
      console.log(`JS已加载: ${path}`)
      return
    }

    // 并行加载当前JS
    const promise = new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = path
      script.type = 'text/javascript'
      script.async = true // 并行加载

      script.onload = () => {
        console.log(`JS加载成功: ${path}`)
        resolve(path)
      }

      script.onerror = () => {
        console.error(`JS加载失败: ${path}`)
        reject(new Error(`JS加载失败: ${path}`))
      }

      document.head.appendChild(script)
    })

    loadPromises.push(promise)
  })

  return Promise.all(loadPromises)
}

/**
 * 并行加载多个CSS文件（public目录下）
 * @param {string[]} cssPaths - CSS路径数组（绝对路径，如 ['/css/a.css', '/css/b.css']）
 * @returns {Promise} - 所有CSS加载完成的Promise
 */
export const loadStyles = (cssPaths) => {
  const paths = Array.isArray(cssPaths) ? cssPaths : [cssPaths]
  const loadPromises = []

  paths.forEach((path) => {
    // 检查是否已加载（防重复）
    if (document.querySelector(`link[href="${path}"]`)) {
      console.log(`CSS已加载: ${path}`)
      return
    }

    // 并行加载当前CSS
    const promise = new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.href = path
      link.rel = 'stylesheet'
      link.type = 'text/css'

      link.onload = () => {
        console.log(`CSS加载成功: ${path}`)
        resolve(path)
      }

      // CSS无onerror，用超时判断失败
      setTimeout(() => {
        if (!document.querySelector(`link[href="${path}"]`)) {
          reject(new Error(`CSS加载失败: ${path}`))
        }
      }, 3000)

      document.head.appendChild(link)
    })

    loadPromises.push(promise)
  })

  return Promise.all(loadPromises)
}

/**
 * 批量删除已加载的JS
 * @param {string[]} jsPaths - 要删除的JS路径数组
 */
export const removeScripts = (jsPaths) => {
  const paths = Array.isArray(jsPaths) ? jsPaths : [jsPaths]
  paths.forEach((path) => {
    const script = document.querySelector(`script[src="${path}"]`)
    if (script) {
      document.head.removeChild(script)
      console.log(`JS已删除: ${path}`)
    }
  })
}

/**
 * 批量删除已加载的CSS
 * @param {string[]} cssPaths - 要删除的CSS路径数组
 */
export const removeStyles = (cssPaths) => {
  const paths = Array.isArray(cssPaths) ? cssPaths : [cssPaths]
  paths.forEach((path) => {
    const link = document.querySelector(`link[href="${path}"]`)
    if (link) {
      document.head.removeChild(link)
      console.log(`CSS已删除: ${path}`)
    }
  })
}
//加载
export function loadSDK(jsPaths, cssPaths) {
  return new Promise((resolve, reject) => {
    loadStyles(cssPaths)
    loadScripts(jsPaths).then(() => {
      resolve()
    })
  })
}
